package indiwood.controller;

import indiwood.model.ProductionCommentVO;
import indiwood.model.ProductionVO;
import indiwood.model.service.ProductionService;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

@Controller
@RequestMapping("/production")
public class ProductionController extends MultiActionController {
	@Resource(name = "productionServiceImpl")
	private ProductionService productionService;
	@RequestMapping("/list.do")
	public ModelAndView list(String pageNo){
		return new ModelAndView("list.production","productionVO",productionService.productionList(pageNo));
	}
	@RequestMapping("/write_view.do")
	public ModelAndView write_view(){
		return new ModelAndView("write_view.production");
	}
	@Resource(name = "uploadPath")
	private String path;
	
	@RequestMapping(value="/write.do",method = RequestMethod.POST)
	public ModelAndView write(ProductionVO pvo, MultipartFile uploadFile){
	if (uploadFile != null && !uploadFile.isEmpty()) {
			File copyFile = new File(path + "production\\"
					+ uploadFile.getOriginalFilename());
			pvo.setImagePath(uploadFile.getOriginalFilename());
			try {
				uploadFile.transferTo(copyFile);
			} catch (IllegalStateException | IOException e) {
				e.printStackTrace();
			}
		}
		productionService.write(pvo);
		return new ModelAndView("redirect:contentNoHit.do?productionNo="+pvo.getProductionNo());
	}

	@RequestMapping("/contentNoHit.do")
	public ModelAndView contentNoHit(int productionNo) {			
		return new ModelAndView("content.production","contentMap",productionService.contentNoHit(productionNo));
	}
	@RequestMapping("/deleteBoard.do")
	public ModelAndView deleteBoard(String no) {		
		productionService.deleteBoard(no);
		return new ModelAndView("list.production","productionVO",productionService.productionList("1"));
	}
	@RequestMapping("/updateView.do")
	public ModelAndView updateView(int no) {
		return new ModelAndView("update.production","contentMap",productionService.contentNoHit(no));
	}
	@RequestMapping("/updateBoard.do")
	public ModelAndView updateBoard(ProductionVO pvo) {
		productionService.updateBoard(pvo);
		return new ModelAndView("content.production","contentMap",productionService.contentNoHit(pvo.getProductionNo()));
	}
	@RequestMapping("/commentWrite.do")
	public ModelAndView commentWrite(ProductionCommentVO pcvo){
		productionService.commentWrite(pcvo);
		return new ModelAndView("redirect:content.do?productionNo="+pcvo.getProductionNo());
	}
	@RequestMapping("/content.do")
	public ModelAndView content(int productionNo,
			@CookieValue(value="productionC",required=false) String cookieValue,HttpServletResponse response) {		
		HashMap<String, Object> contentMap=new HashMap<String, Object>();
		if(cookieValue==null){
			response.addCookie(new Cookie("productionC","|"+productionNo+"|"));
			contentMap=productionService.content(productionNo);
		}else if(cookieValue.indexOf("|"+productionNo+"|")==-1){
			cookieValue+="|"+productionNo+"|";
			response.addCookie(new Cookie("productionC",cookieValue));
			contentMap=productionService.content(productionNo);
		}else{
			contentMap = productionService.contentNoHit(productionNo);
		}
		return new ModelAndView("content.production","contentMap",contentMap);
	}
	@RequestMapping("/deleteComment.do")
	public ModelAndView deleteComment(String no){
		productionService.deleteComment(no);
		return new ModelAndView("content.production");
	}
}
